﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.ServiceModel.Configuration;
using log4net;

namespace WinServiceDemo
{

    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // 启动所有WCF服务
            //StartAllConfiguredServices();
            startArvOp();
        }
        static ServiceHost host = new ServiceHost(typeof(ZY.EntityFrameWork.WcfSvcLib.Impl.WcfArvOpSvc));
        static ILog log = LogManager.GetLogger("a");
        private static void startArvOp()
        {
            try
            {
                log.Info("准备启动服务！");
                log.Error("准备启动服务！");
                host.Open();

            }
            catch (Exception ex)
            {
                
                log.Error(ex.Message + "_____" + ex.InnerException,ex);
            }
            
        }

        protected override void OnStop()
        {
            // 关闭所有WCF服务
            //CloseAllServices();
            try
            {
                host.Close();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + "_____" + ex.InnerException, ex);
            }
            
        }


        static List<ServiceHost> _hosts = new List<ServiceHost>();


        //  开启服务
        private static void OpenHost(Type t)
        {
            ServiceHost hst = new ServiceHost(t);

            hst.Open();

            _hosts.Add(hst);
        }


        // 启动所有服务
        public static void StartAllConfiguredServices()
        {
            // 从配置文件中读取服务接口信息
            Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
            ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");

            string AssemblyStr = ", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";

            foreach (ServiceElement el in svcmod.Services.Services)
            {
                // 获取服务接口的类型
                Type svcType = Type.GetType(el.Name + ",ZY.EntityFramework" + AssemblyStr, true, true);
                //Type svcType = Type.GetType(el.Name + "," + el.Name.Split('.')[0] + AssemblyStr, true, true);
                if (svcType == null)
                    throw new Exception("Invalid Service Type " + el.Name + " in configuration file.");

                // 开启服务
                OpenHost(svcType);

                Console.WriteLine("Start Service{0}", el.Name);
            }
        }

        // 关闭所有服务
        public static void CloseAllServices()
        {
            foreach (ServiceHost hst in _hosts)
            {
                hst.Close();
            }
        }
    }
}
